< previous page page_284 next page >

Page 284
e10a6a0a59f035e870d90cca05b85fce.gif
    ByVal length As Single, ByVal theta As Single)
Dim x1 As Integer
Dim y1 As Integer

    ' See where this branch should end.
    x1 = X + length * Cos(theta)
    y1 = Y + length * Sin(theta)
    picCanvas.Line (X, Y)-(x1, y1)

    ' If depth > 1, draw the attached branches.
    If depth > 1 Then
        DrawBranch depth - 1, x1, y1, _
            length * LENGTH_SCALE, theta + DTHETA
        DrawBranch depth - 1, x1, y1, _
            length * LENGTH_SCALE, theta - DTHETA
    End If
End Sub
Example program BinTree uses this routine to draw binary trees. Enter the depth of the tree and click the Go button to make the program draw the tree.
There are several modifications you can make to program BinTree to make it more interesting. First, you can adjust the scale factor LENGTH_SCALE by which the branches at each level are shortened. If you increase LENGTH_SCALE, you get a bushier tree like the one on the left in Figure 10.3. You can also change the value of DTHETA that determines the angle new branches make with the branches below.
Figure 10.4 shows a tree in which DTHETA is set to 90 degrees. You can also vary the thickness of branches, making each smaller than its parent.
Example program BinTree2 is similar to program BinTree except it allows you to change the values of LENGTH_SCALE and DTHETA. If you check the Taper Branches box, the program makes each branch a little thinner than the previous one.
z0304-01.gif
Figure 10.3
Depth eight trees with LENGTH_SCALE = 0.95 (left) and 0.75 (right).

 
< previous page page_284 next page >